//
//  PublishViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/26.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "PublishViewController.h"
#import "MessageViewController.h"//消息Controller
#import "MineViewController.h"//我的Controller
#import "PublishNewViewController.h"//发布新的订单Controller
#import "UnfinishedTableViewCell.h"//未完成Cell
#import "DonePublishTableViewCell.h"//已完成Cell

#import "PublishOrderDetailViewController.h"//发布订单详情Controller

@interface PublishViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *unfinishedTableView;
@property (nonatomic, strong) UITableView *doneTableView;

@end

@implementation PublishViewController
{
    UIScrollView *_scrollView;
    UIButton *_unfinished;
    UIButton *_done;
    UIView *_slider;
}
#define SliderWidth (SCREEN_WIDTH / 2)

 static NSString *const UnfinishedTableViewCellID = @"UnfinishedTableViewCell";
 static NSString *const DonePublishTableViewCellID = @"DonePublishTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航栏
    [self setNav];
    
    //设置HeaderView&ScrollView
    [self setHeaderViewandScrollView];
    //注册tableView
    [self registerTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    UIButton *mine = [UIButton buttonWithType:UIButtonTypeCustom];
    mine.frame = CGRectMake(0, 0, 59, 44);
    [mine addTarget:self action:@selector(jumpToMine) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *mineImage;
    //6p
    if (SCREEN_WIDTH == 414){
        mineImage = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 7.5, 29, 29)];
    }else{
        mineImage = [[UIImageView alloc] initWithFrame:CGRectMake(-1, 7.5, 29, 29)];
    }
    mineImage.image = [UIImage imageNamed:@"top-my"];
    mineImage.layer.cornerRadius = 14.5;
    mineImage.layer.masksToBounds = YES;
    mineImage.alpha = 0.9;
    [mine addSubview:mineImage];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:mine];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    UIButton *message = [UIButton buttonWithType:UIButtonTypeCustom];
    message.frame = CGRectMake(0, 0, 59, 44);
    [message addTarget:self action:@selector(jumpToMessage) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *messageImage;
    //6p
    if (SCREEN_WIDTH == 414){
        messageImage = [[UIImageView alloc] initWithFrame:CGRectMake(35, 7.5, 29, 29)];
    }else{
        messageImage = [[UIImageView alloc] initWithFrame:CGRectMake(31, 7.5, 29, 29)];
    }
    messageImage.image = [UIImage imageNamed:@"top-message"];
    messageImage.layer.cornerRadius = 14.5;
    messageImage.layer.masksToBounds = YES;
    messageImage.alpha = 0.9;
    [message addSubview:messageImage];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:message];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 76, 30)];
    lable.text = @"我的发布";
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = [UIColor whiteColor];
    lable.font = [UIFont fontWithName:APPFONT size:19];
    self.navigationItem.titleView = lable;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 59, 44)];
    view.backgroundColor = [UIColor clearColor];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    backItem.title = @"";
    self.navigationItem.backBarButtonItem = backItem;
}

#pragma mark === 设置HeaderView&ScrollView ===
- (void)setHeaderViewandScrollView{
    //HeaderView
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 98.5)];
    headerView.backgroundColor = RGBCOLOR(236, 236, 236);
    [self.view addSubview:headerView];
    
    UIButton *newDelivery = [UIButton buttonWithType:UIButtonTypeCustom];
    newDelivery.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    newDelivery.backgroundColor = [UIColor whiteColor];
    [newDelivery setTitle:@"发布新的派送" forState:UIControlStateNormal];
    newDelivery.titleLabel.font = [UIFont fontWithName:APPFONT size:15];
    [newDelivery setTitleColor:RGBCOLOR(255, 82, 82) forState:UIControlStateNormal];
    [newDelivery addTarget:self action:@selector(publishNewDelivery) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:newDelivery];
    
    _unfinished = [UIButton buttonWithType:UIButtonTypeCustom];
    _unfinished.frame = CGRectMake(0, 54, SCREEN_WIDTH / 2, 54);
    _unfinished.backgroundColor = [UIColor whiteColor];
    [_unfinished setTitle:@"未完成" forState:UIControlStateNormal];
    _unfinished.titleLabel.font = [UIFont fontWithName:APPFONT size:15];
    [_unfinished setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [_unfinished addTarget:self action:@selector(being) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_unfinished];
    
    _done = [UIButton buttonWithType:UIButtonTypeCustom];
    _done.frame = CGRectMake(SCREEN_WIDTH / 2, 54, SCREEN_WIDTH / 2, 54);
    _done.backgroundColor = [UIColor whiteColor];
    [_done setTitle:@"已完成" forState:UIControlStateNormal];
    _done.titleLabel.font = [UIFont fontWithName:APPFONT size:15];
    [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [_done addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_done];
    
    _slider = [[UIView alloc] initWithFrame:CGRectMake(0, 97, SliderWidth, 1)];
    _slider.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:_slider];
    UIView *sliderr = [[UIView alloc] initWithFrame:CGRectMake(39, 0, SliderWidth - 39 * 2, 1)];
    sliderr.backgroundColor = APPCOLOR;
    [_slider addSubview:sliderr];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 98, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = RGBACOLOR(0, 0, 0, 0.1);
    [headerView addSubview:line];
    
    //ScrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 98.5, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, SCREEN_HEIGHT - 44 - 64 - 49 - 54);
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = RGB(236, 236, 236);
    [self.view addSubview:_scrollView];
}
- (void)publishNewDelivery{
    PublishNewViewController *publish = [[PublishNewViewController alloc] init];
    publish.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:publish animated:YES];
}
- (void)being{
    [_unfinished setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [UIView animateWithDuration:0.2 animations:^{
        _slider.frame = CGRectMake(0, 97, SliderWidth, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(0, 0);
    }];
}
- (void)done{
    [_unfinished setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [_done setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [UIView animateWithDuration:0.2 animations:^{
        _slider.frame = CGRectMake(SCREEN_WIDTH / 2, 97, SliderWidth, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        CGFloat x = scrollView.contentOffset.x / 2;
        if (scrollView.contentOffset.x <= 0) {
            _slider.frame = CGRectMake(0, 97, SliderWidth, 1);
        }else if (scrollView.contentOffset.x >= SCREEN_WIDTH){
            _slider.frame = CGRectMake(SCREEN_WIDTH / 2, 97, SliderWidth, 1);
        }else{
            _slider.frame = CGRectMake(x, 97, SliderWidth, 1);
        }
        if (scrollView.contentOffset.x > SliderWidth) {
            [_unfinished setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
            [_done setTitleColor:APPCOLOR forState:UIControlStateNormal];
        }else{
            [_unfinished setTitleColor:APPCOLOR forState:UIControlStateNormal];
            [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
        }
    }
}

- (void)registerTableView{
    [self.unfinishedTableView registerNib:[UINib nibWithNibName:@"UnfinishedTableViewCell" bundle:nil] forCellReuseIdentifier:UnfinishedTableViewCellID];
    [self.doneTableView registerNib:[UINib nibWithNibName:@"DonePublishTableViewCell" bundle:nil] forCellReuseIdentifier:DonePublishTableViewCellID];
}

#pragma mark === UITableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _unfinishedTableView) {
        return 3;
    }else return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //未完成
    if (tableView == _unfinishedTableView) {
        UnfinishedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UnfinishedTableViewCellID];
//        [cell loadData:<#(UnfinishedModel *)#>];
        
        if (indexPath.row == 0) {
            cell.state.text = @"未接单";
            cell.contact.hidden = YES;
            cell.cancle.hidden = NO;
        }else{
            cell.state.text = @"进行中";
            cell.contact.hidden = NO;
            cell.cancle.hidden = YES;
        }
        cell.cellClick = ^(int num){
            switch (num) {
                case 0:
                    DLog(@"联系骑手");
                    break;
                case 1:
                    DLog(@"取消发布");
                    break;
                default:
                    break;
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    //已完成
    else{
        DonePublishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DonePublishTableViewCellID];
//        [cell loadData:<#(DonePublishModel *)#>];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PublishOrderDetailViewController *publish = [[PublishOrderDetailViewController alloc] init];
    publish.hidesBottomBarWhenPushed = YES;
    if (tableView == _unfinishedTableView) {
        if (indexPath.row == 0) {
            publish.state = 0;
        }else if (indexPath.row == 1){
            publish.state = 1;
        }else{
            publish.state = 2;
        }
    }else{
        publish.state = 3;
    }
    [self.navigationController pushViewController:publish animated:YES];
}

- (void)jumpToMine{
    DLog(@"我的");
    //    LoginWithPhoneViewController *login = [[LoginWithPhoneViewController alloc] init];
    //    login.hidesBottomBarWhenPushed = YES;
    //    [self.navigationController pushViewController:login animated:YES];
    MineViewController *mine = [[MineViewController alloc] init];
    mine.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mine animated:YES];
}
- (void)jumpToMessage{
    DLog(@"消息");
    MessageViewController *message = [[MessageViewController alloc] init];
    message.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:message animated:YES];
}

- (UITableView *)unfinishedTableView{
    if (!_unfinishedTableView) {
        _unfinishedTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 98.5 - 49)style:UITableViewStylePlain];
        _unfinishedTableView.delegate = self;
        _unfinishedTableView.dataSource = self;
        _unfinishedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _unfinishedTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _unfinishedTableView.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:_unfinishedTableView];
    }
    return _unfinishedTableView;
}
- (UITableView *)doneTableView{
    if (!_doneTableView) {
        _doneTableView= [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH,0, SCREEN_WIDTH, SCREEN_HEIGHT - 98.5 - 64 - 49)style:UITableViewStylePlain];
        _doneTableView.delegate = self;
        _doneTableView.dataSource = self;
        _doneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _doneTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _doneTableView.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:_doneTableView];
    }
    return _doneTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
